package com.hpin.fansea.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.MyRepairsAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.MyRepairs;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的报修记录
 * Created by Aaron on 15/9/7.
 */
public class MyRepairsActivity extends BaseActivity implements View.OnClickListener {

    private PullToRefreshListView listView;

    private MyRepairsAdapter adapter;

//    private String totalCount;// 维修信息的总条数
    private int pageIndex = 0;
    private List<MyRepairs.RepairDetail> repairList = new ArrayList<MyRepairs.RepairDetail>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_repair);

        if (TextUtils.isEmpty(sp.getString(Constant.houseId, ""))) {
            showToast("未认证用户");
            return;
        }

        initWidget();

        loadDataFromNet();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.my_repair_record));

        iv_left.setOnClickListener(this);

        listView = (PullToRefreshListView) findViewById(R.id.my_repairs_list);

        adapter = new MyRepairsAdapter(repairList, this);

        listView.setAdapter(adapter);

        listView.setMode(PullToRefreshBase.Mode.BOTH);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(MyRepairsActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                pageIndex=0;
                repairList.clear();
                loadDataFromNet();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(MyRepairsActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                loadDataFromNet();

            }
        });
    }

    /**
     * 网络获取数据
     */
    private void loadDataFromNet() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT
                + Constant.GETMAINTAINLIST + "/" + sp.getString(Constant.houseId, "")
                + "?token=" + sp.getString(Constant.token, "") + "&pageSize="
                + Constant.PAGECOUNT + "&pageIndex=" + (++pageIndex)
                + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                listView.onRefreshComplete();

                try {

                    MyRepairs myRepairs = JSONObject.parseObject(str, MyRepairs.class);

                    if (Constant.STATUS_SUCESS.equals(myRepairs.status)) {

                        setListView(myRepairs.result);
                        adapter.setRepairDetailList(repairList);
                        adapter.notifyDataSetChanged();

                    } else {
                        showToast(myRepairs.message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("获取数据失败");
                }

            }
        }, new HttpUtil.FailListener() {
            @Override
            public void setOnFailListener() {
                listView.onRefreshComplete();
            }
        });

    }


    /**
     * 判断是否有重复并更新
     */
    private void setListView(List<MyRepairs.RepairDetail> tempGuideLists) {

        if (tempGuideLists == null || tempGuideLists.size() == 0)
            return;

        if (repairList.size() == 0) {

            repairList.addAll(tempGuideLists);
            return;
        }

        for (MyRepairs.RepairDetail goods : tempGuideLists) {
            boolean exist = false;
            for (MyRepairs.RepairDetail g : repairList) {
                if (goods.id_.equals(g.id_)) {
                    exist = true;
                    break;
                }
            }
            if (!exist)
                repairList.add(goods);
        }

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;

            default:
                break;
        }
    }
}
